package cn.wyu.dao;

import cn.wyu.pojo.Article;
import cn.wyu.util.JDBCUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ArticleDaoImpl implements ArticleDao {
    @Override
    public List<Article> findByTypeCode(String type_code) {

        //要去数据库查询
        String sql = "select  ID, TYPE_CODE, TITLE, SUPPLIER, PRICE, (PRICE*DISCOUNT)DISCOUNT, LOCALITY, PUTAWAY_DATE, STORAGE, IMAGE, DESCRIPTION, CREATE_DATE, DISABLED from ec_article where TYPE_CODE like ?;";
        //查找用executeQuery
        ResultSet rs = JDBCUtil.executeQuery(sql, type_code+"%");//集合
        //2.将得到的数据封装到一个Article

        List<Article> list = new ArrayList<>();
        //遍历出数据
        try{
            while(rs.next()){
                //得到数据

                Article at = new Article();
                at.setId(rs.getInt("ID"));
                at.setTypeCode(rs.getString("TYPE_CODE"));
                at.setTitle(rs.getString("TITLE"));
                at.setSupplier(rs.getString("SUPPLIER"));
                at.setPrice(rs.getString("PRICE"));
                at.setDiscount(String.format("%.2f",rs.getDouble("DISCOUNT")));//只保留两位小数
                at.setLocality(rs.getString("LOCALITY"));
                at.setPutawayDate(rs.getString("PUTAWAY_DATE"));
                at.setStorage(rs.getInt("STORAGE"));
                at.setImage(rs.getString("IMAGE"));
                at.setDescription(rs.getString("DESCRIPTION"));
                at.setCreateDate(rs.getString("CREATE_DATE"));
                at.setDisabled(rs.getInt("DISABLED"));

                //3.将对象放到集合中list集合中
                list.add(at);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return list;

    }

    //通过id查询商品
    @Override
    public Article findByArticleID(String articleID) {

        String sql = "select * from ec_article where id=?";
        ResultSet rs = JDBCUtil.executeQuery(sql,articleID);
        Article at = new Article();
        try{
            if(rs.next())
            {
                at.setId(rs.getInt("ID"));
                at.setTypeCode(rs.getString("TYPE_CODE"));
                at.setTitle(rs.getString("TITLE"));
                at.setSupplier(rs.getString("SUPPLIER"));
                at.setPrice(rs.getString("PRICE"));
                at.setDiscount(String.format("%.2f",rs.getDouble("DISCOUNT")));//只保留两位小数
                at.setLocality(rs.getString("LOCALITY"));
                at.setPutawayDate(rs.getString("PUTAWAY_DATE"));
                at.setStorage(rs.getInt("STORAGE"));
                at.setImage(rs.getString("IMAGE"));
                at.setDescription(rs.getString("DESCRIPTION"));
                at.setCreateDate(rs.getString("CREATE_DATE"));
                at.setDisabled(rs.getInt("DISABLED"));

            }
        }catch (SQLException throwables){
            throwables.printStackTrace();
        }

        System.out.println("对应商品是："+at);
        return at;
    }
}
